package com.qiji.video.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiji.video.bean.BrowsingHistory;
import com.qiji.video.bean.Collection;
import com.qiji.video.bean.Users;
import com.qiji.video.bean.Videos;
import com.qiji.video.bean.vo.BrowsingHistoryAndVideo;
import com.qiji.video.bean.vo.CollectionAndVideo;
import com.qiji.video.mapper.BrowsingHistoryMapper;
import com.qiji.video.mapper.CollectionMapper;
import com.qiji.video.mapper.UsersMapper;
import com.qiji.video.mapper.extend.UserControlExtendMapper;
import com.qiji.video.service.IUserControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserControlServiceImpl implements IUserControlService {
    @Autowired
    private UsersMapper mapper;
    @Autowired
    private UserControlExtendMapper extendMapper;
    @Autowired
    private CollectionMapper collectionMapper;
    @Autowired
    private BrowsingHistoryMapper browsingHistoryMapper;
    @Override
    public PageInfo<Users> pageQuery(Integer pageNum, Integer pageSize) throws SecurityException{
        PageHelper.startPage(pageNum,pageSize);
        List<Users> users = extendMapper.selectUser();
        return new PageInfo<>(users);
    }

    @Override
    public PageInfo<Users> selectUserById(Integer pageNum, Integer pageSize,Integer id) throws SecurityException{
        PageHelper.startPage(pageNum,pageSize);
        if (id == null){
            List<Users> users = extendMapper.selectUser();
            return new PageInfo<>(users);
        }
        List<Users> list = extendMapper.selectByPrimaryKey(id);
        if (list == null){
            throw new SecurityException("没有该用户id");
        }
        return new PageInfo<>(list);
    }

    @Override
    public void updateUserMagsById(Users users) throws SecurityException{
        mapper.updateByPrimaryKeySelective(users);
    }

    @Override
    public void upadateUserPasswordById(Users users) {
        mapper.updateByPrimaryKeySelective(users);
    }

    @Override
    public void daleteUserById(Integer id) {
        Users users = mapper.selectByPrimaryKey(id);
        if (users == null){
            throw new SecurityException("删除失败，不存在该用户id");
        }
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insertUser(Users users) throws SecurityException{
        System.out.println(users);
        int i = mapper.insertSelective(users);
        if (i == 0){
            throw new SecurityException("添加失败");
        }
    }

    @Override
    public List<BrowsingHistoryAndVideo> selectBVById(Integer uid) {
        List<BrowsingHistoryAndVideo> list = extendMapper.selectBAByid(uid);
        return list;
    }

    @Override
    public List<Videos> selectBHById(Integer id) {
        return extendMapper.selectBHById(id);
    }
    public List<BrowsingHistory> selectBhTimeById(Integer id){
        return extendMapper.selectBhTimeById(id);
    }

    @Override
    public List<CollectionAndVideo> selectCAById(Integer uid) {
        List<CollectionAndVideo> list = extendMapper.selectCVByid(uid);
        return list;
    }


    @Override
    public List<Videos> selectCLById(Integer id) {
        return extendMapper.selectCLById(id);
    }
    public List<Collection> selectClTimeById(Integer id){
        return extendMapper.selectClTimeById(id);
    }
    @Override
    public List<Users> selsct() {
        List<Users> users = extendMapper.selectUser();
        return users;
    }

    @Override
    public void delectBHById(Integer id) {
        browsingHistoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void delectCLById(Integer id) {
        collectionMapper.deleteByPrimaryKey(id);
    }
}
